*This file creates permutated shocks to be used in zhift-share robustness following Borusyak and Hull (2024). Table  D.18.

forvalues i = 1(1)999 {
use  "$temp\repadata.dta" , clear
destring ind39, replace
drop repa_scaled

keep if ind39>5&ind39<40 // permutate shocks for manufactruing industries

*Need to keep the seed same to replicate the paper results.
*Need separate seed for each shuffle
set seed 2`i'

shufflevar reparations

*Generate counterfactual reparation shocks
gen repa_scaled=reparations_shuffled/individual

joinby ind39 using "$temp\Long_shares.dta" 

*Generate counterfactual Bartik shocks
gen bartik1=repa_scaled*share_ind

keep mun39 ind39 bartik1

reshape wide bartik1 ,i(mun39) j(ind39) 

egen bartik_`i'=rowtotal(bartik1*)

keep mun39  bartik_`i'

tempfile s`i'
save `s`i''
 }

use `s1', clear
    forvalues i = 2(1)999 {
	joinby mun39 using `s`i''
	 }

save "$temp\counterfactual_bartik.dta", replace
